/*
 * @Description: 配置
 * @Author: zhangchangcheng
 * @Date: 2022-01-14 14:46:49
 * @LastEditors: chenju
 * @LastEditTime: 2023-05-08 11:16:31
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import { getFactorList } from '@/apis/system/database/factorList'
import type { GetFactorListParams } from '@/apis/system/database/model/factorListModel'
import type { CTableProps } from '@/components/CTable/index'
import type { BasicPagingListParams } from '@/apis/basicModel'
import type { SearchOptionsType, TableFieldsSettingType } from '@/components/CPageTable'
import { ColumnSettingTypeEnum } from '@/components/CPageTable'
import baseConfig from '@/config'

export const useFactorList = () => {
  // 导入因子弹框
  // 搜索字段及其他
  const searchForm = reactive<GetFactorListParams>({
    factorNameOrNo: undefined,
    category: null,
    factorSource: null,
    country: undefined,
    comment: undefined,
    tag: undefined
  })
  // 搜索配置
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '因子名称或编号',
      field: 'factorNameOrNo',
      type: 'input'
    },
    {
      name: '因子类别',
      field: 'category',
      type: 'select',
      fixedOption: 'FactorCategoryOptions',
      selectOption: {
        autoWidth: true
      }
    },
    {
      name: '国别（地区）',
      field: 'country',
      type: 'input'
      // isFold: true
    },
    {
      name: '因子来源',
      field: 'factorSource',
      type: 'select',
      fixedOption: 'FactorSourceOptions'
    },
    {
      name: '因子标签',
      field: 'tag',
      type: 'input'
    },
    {
      name: '备注',
      field: 'comment',
      type: 'input'
    }
  ])

  // 表格配置
  const tableOptions: CTableProps = {
    loadData,
    selection: false,
    border: false
  }

  function loadData(parameter: BasicPagingListParams) {
    const params: GetFactorListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getFactorList(params)
  }
  // 处理参数
  const handleParams = (): GetFactorListParams => {
    // 参数
    let params: GetFactorListParams = {
      factorNameOrNo: searchForm.factorNameOrNo,
      category: searchForm.category,
      comment: searchForm.comment,
      factorSource: searchForm.factorSource,
      country: searchForm.country,
      tag: searchForm.tag
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: TableFieldsSettingType[] = [
    {
      title: '因子名称/英文名称/因子编号',
      field: 'factorName',
      minWidth: 250,
      slot: 'name',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '因子类别/国别',
      field: 'category',
      slot: 'categorySlot',
      width: 120,
      columnSettingConfig: {
        enabled: true
        // type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '碳排放因子/因子来源',
      field: 'factorSource',
      slot: 'factorSourceSlot',
      minWidth: 200,
      columnSettingConfig: {
        enabled: true
        // type: ColumnSettingTypeEnum.REQUIRED
      }
    },
    {
      title: '基准年',
      field: 'baseYear',
      width: 80,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '标签',
      field: 'tag',
      slot: 'tag',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '备注',
      field: 'comment',
      slot: 'comment',
      minWidth: 150,
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作',
      field: 'op',
      slot: 'op',
      width: 100,
      fixed: 'right',
      columnSettingConfig: {
        enabled: true,
        type: ColumnSettingTypeEnum.FIXED
      }
    }
  ]

  return {
    // 头部右侧筛选项配置
    searchOptions,
    // 筛选项字段
    searchForm,
    // 表格配置
    tableOptions,
    tableFieldsSetting
  }
}
